<?php
namespace soa\parter\sto;
use artisan\http;
use artisan\input;

class RealName{
    /**
     * 服务地址
     */
    protected $service_url = '';

    /**
     * 服务名称
     */
    protected $service_name = 'kuaidihelp_msd';

    /**
     * 传输校验的key值
     */
    protected $key = '0bdbb2c325525e986d55ef50ca66d056';

    /**
     * 登录用户名
     */
    protected $loginAcct = '9000000002';

    /**
     * 登录密码
     */
    protected $password = 'qq123456';

    /**
     * 接口地址
     */
    protected $url = [
        //正式接口
        'ZJ'      => 'http://103.36.134.11:8080/sto-ms-api/photoPushController',   //总部浙江实名制接口
        'LOGIN' =>'http://pda.sto.cn:2225/mdmweb/realnameInterface/applyLogin.action',  //登录接口
        'PERSON_IDENTITY' =>'http://pda.sto.cn:2225/mdmweb/realnameInterface/individualAddInterface.action',  //个人用户信息录入
        'CUSTOMER_WABILL' =>'http://pda.sto.cn:2225/mdmweb/realnameInterface/customerWabillAddInterface.action',  //客户运单录入
        'REALNAME' => 'http://pda.sto.cn:2225/mdmweb/realnameInterface/realnameSendInterface.action',  //实名寄递录入
        'GET_USER' =>'http://pda.sto.cn:2225/mdmweb/realnameInterface/findIndividualByIdCard.action',  //用户信息查询

        //测试接口
        'ZJ_TEST' => 'http://116.228.73.38:8010/sto-ms-api/photoPushController',  //总部浙江实名制接口 测试
        'LOGIN_TEST' => 'http://116.228.73.38:2225/mdmweb/realnameInterface/applyLogin.action',  //登录接口 测试
        'PERSON_IDENTITY_TEST' => 'http://116.228.73.38:2225/mdmweb/realnameInterface/individualAddInterface.action',  //个人用户信息录入 测试
        'CUSTOMER_WABILL_TEST' => 'http://116.228.73.38:2225/mdmweb/realnameInterface/customerWabillAddInterface.action',  //客户运单录入 测试
        'REALNAME_TEST' => 'http://116.228.73.38:2225/mdmweb/realnameInterface/realnameSendInterface.action',  //实名寄递录入 测试
        'GET_USER_TEST' => 'http://116.228.73.38:2225/mdmweb/realnameInterface/findIndividualByIdCard.action',  //用户信息查询 测试
    ];


    public function __construct($urlName)
    {
        if(empty($urlName) && !in_array($urlName,$this->url)){
            return null;
        }
        $this->service_url = $this->url[$urlName];
        return $this;
    }


    /**
     * 获取服务名称
     */
    public function getServiceName()
    {
        return $this->service_name;
    }

    /**
     * 获取服务地址
     */
    public function getServiceUrl()
    {
        return $this->service_url;
    }

    /**
     * 获取校验key
     */
    public function getKey()
    {
        return $this->key;
    }

    /**
     * 获取登录账号
     */
    public function getLoginAcct()
    {
        return $this->loginAcct;
    }

    /**
     * 获取登录密码
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * 发送请求
     * @param $data
     * @return mixed
     */
    public function send($data)
    {
        $options = [
            'HTTPHEADER' => ["Content-Type: text/html;charset=UTF-8"]
        ];
        return http::post($this->getServiceUrl(),json_encode($data),$options);
    }

    /**
     * 发送get请求
     * @param $data
     * @return mixed
     */
    public function sendGet($data)
    {
        $options = [
            'HTTPHEADER' => ["Content-Type: text/html;charset=UTF-8"]
        ];
        return http::get($this->getServiceUrl(),$data,$options);
    }

    /**
     * 发送请求
     * @param $data
     * @return mixed
     */
    public function sendPost($data)
    {
        $options = [
//            'HTTPHEADER' => ["Content-Type: text/html;charset=UTF-8"]
            'HTTPHEADER' => ["Content-Type: application/x-www-form-urlencoded; charset=UTF-8"]
        ];
        return http::post($this->getServiceUrl(),$data,$options);
    }

    /**
     * 登录
     * @return bool
     */
    public function login()
    {
        $sessionid = \artisan\cache::get('sto:sessionId');  //从缓存中获取sessionid
        if($sessionid && strlen($sessionid) == 36){     //session 存在、格式正确 直接返回sessionid
            return $sessionid;
        }
        $sessionid = $this->tryLogin();
        if(empty($sessionid)){   //登录失败
            return false;
        }
        $result = \artisan\cache::set('sto:sessionId',$sessionid,20*3600);    //将sessionid写入缓存
        if($result != true){
            logDebug('sto login is failed','check the sto login');
        }
        return $sessionid;
    }

    /**
     * 递归尝试登录 5次
     * @param int $i
     * @return bool
     */
    public function tryLogin($i=0)
    {
        $data = array(
            'loginAcct'=>$this->getLoginAcct(),
            'password'=>$this->getPassword(),
            'dataSource'=>'03'
        );
        $response = json_decode($this->send($data),true);
        logDebug(json_encode($response),'tryLogin');
        if($response['result'] != 0 || strlen($response['sessionid']) != 36){   //登录失败
            if($i<5){
                $this->tryLogin(++$i);
            }else{  //若尝试登录5次后 无法获取到正确的sessionid则返回false
                return false;
            }
        }
        return $response['sessionid'];
    }


    /**
     * 个人用户信息录入
     * @param $data
     * @return mixed
     */
    public function uploadIdCard($data)
    {
        $param = array(
            'sessionid'       => $data['sessionid'],
            'name'            => $data['name'],         //姓名
            'sex'             => $data['sex'] == '男' ? 1 : 2,   //性别
//            'valididcardCode'=>$data['idno'],         //身份证件号码
            'valididcardCode' => $data['cardID'],       //身份证件号码
//            'mobilephone'=>$data['mobile'],           //移动电话
            'mobilephone'     => $data['sendPhone'],    //移动电话
            'telephone'       => '',                    //固定电话
            'addressProvince' => $data['province'],     //地址-省(自治区，直辖市)
            'addressCity'     => $data['city'],         //地址-市
            'addressCounty'   => $data['district'],     //地址-县
            'addressDetailed' => $data['address'],      //地址-详细地址
            'dataSource'      => '03',                  //数据来源  PDA录入
            'applyAcct'       => $this->getLoginAcct(), //应用帐号
            'userType'        => '1',                   //用户类型  1:非协议用户 2:协议用户
        );
        logDebug('param:'.json_encode($param),'RealName uploadIdCard');
        $ret = $this->send($param);
        logDebug('ret:'.$ret,'RealName uploadIdCard');
        return $ret;
    }

    /**
     * 实名认证查验接口
     * @param $sessionid
     * @param $mobile
     * @return mixed
     */
    public function realNameCheck($sessionid,$mobile)
    {
        if(empty($sessionid) || empty($mobile)){
            return false;
        }
        $param = array(
            'sessionid'=>$sessionid,
            'mobilephone'=>$mobile,  //需要查验的手机号
        );
        return $this->sendGet($param);
    }

    /**
     * 根据身份证号码获取用户信息
     * @param $sessionid
     * @param $idCard
     * @return mixed
     */
    public function getUser($sessionid,$idCard)
    {
        if(empty($sessionid) || empty($idCard)){
            return false;
        }
        $param = array(
            'sessionid'=>$sessionid,
            'idCard'=>$idCard,  //身份证号
        );
        return $this->sendGet($param);
    }

    /**
     * 浙江实名寄递
     * @param $data
     * @return bool|mixed
     */
    public function realNameZj($data)
    {
        if(empty($data)){
            return false;
        }
        $result = $this->sendPost(['baowen'=>json_encode($data,JSON_UNESCAPED_UNICODE)]);
        return $result;
    }
    

    /**
     * 实名寄递录入
     * @param $data
     * @return mixed
     */
    public function uploadOrderInfoOfSto($data)
    {
        if(empty($data['userSign'])){
            return false;
        }
        $param = array(
            'sessionid'=>$data['sessionid'],
            'userSerialNo'=>$data['userSign'],              //用户序列号
            'waybillNo'=>$data['waybillArr'],               //运单号
            'sendProvince'=>$data['sendProvince'],          //寄件地址-省
            'sendCity'=>$data['sendCity'],                  //寄件地址-市
            'sendArea'=>$data['sendDistrict'],              //寄件地址-区
            'sendAddress'=>$data['sendAddress'],            //寄件地详细址
            'receiverName'=>$data['receiverName'],           //收件人姓名
            'receiverMobilephone'=>str_replace('-','',$data['receiverMobilephone']),   //收件人手机号
            'receiverProvince'=>$data['receiverProvince'],   //寄件地址-省
            'receiverCity'=>$data['receiverCity'],           //寄件地址-市
            'receiverArea'=>$data['receiverArea'],       //寄件地址-区
            'receiverAddress'=>$data['receiverAddress'],     //寄件地详细址
            'dataSource'=>'03',                                //数据来源
            'sendOrgCode'=>$data['index_shop_id'],           //寄件网点
            'createUser'=>$data['cm_code'],                   //业务员编码
            'remark'=>'',                                       //备注(选填)

            'name'=>$data['name'],  //姓名
            'sex'=>$data['sex']=='男'?1:2,  //性别 性别 1:男 2:女
            'nationality'=>$data['nation'],  //民族
            'valididcardType'=>1,  //证件类型	有效身份证件类型 1:二代身份证 2:军人(警察)证 3:港澳台通行证  4:护照
            'valididcardCode'=>$data['cardID'],  //证件号码
            'mobilephone'=>$data['sendPhone'],  //手机号
            'userType'=>1,  //用户类型	用户类型 1:非协议用户 2:协议用户
            'isCheck'=>1,  //用户类型	是否完成验视 1:是 2:否
        );
//        http::post('http://api.kuaidihelp.com/v1/MyTest/remoteLogFile',['message'=>$param],['TIMEOUT'=>1]);
        logDebug($param,'realname uploadOrderInfoOfSto');
        return $this->send($param);
    }




}